﻿using System.Data;
using System.Data.SqlClient;
using System;

namespace Adams.Web.Areas.MyAdams.Data.Classifieds
{
    public static class Index
    {
        public static MyAdams.Models.Classifieds.Index.PageLoad PageLoad(int memberId)
        {
            var data = new MyAdams.Models.Classifieds.Index.PageLoad()
            {
                 Postings = new Shared.Entities.Classifieds.PostingCollection()
            };

            using (SqlDataReader dr = Adams.Web.Data.DB.RunDataReader(@"
SELECT P.PostingID,P.CategoryID,P.MemberID,P.StatusID,P.Title,P.Price,P.Location,P.ReplyTo,P.Description,P.CreatedOn,P.ExpiresOn,M.FirstName,M.LastName,M.Email,C.Name As CategoryName,T.TypeID AS TypeID, T.Name AS TypeName
FROM Classifieds.Postings P 
INNER JOIN Classifieds.Categories C ON P.CategoryID = C.CategoryID 
INNER JOIN Classifieds.Types T ON C.TypeID = T.TypeID 
INNER JOIN Membership.Members M ON P.MemberID = M.MemberID
WHERE P.MemberID = @MemberID 
AND P.StatusID <> 4 AND DATEDIFF(d, GETDATE(), P.ExpiresOn) >= -7
ORDER BY ExpiresOn ASC
", CommandType.Text,
    Adams.Web.Data.DB.CreateParameter("@MemberID", memberId)))
            {
                while (dr.Read())
                {
                    var Posting = Adams.Web.Data.DB.InitFromReader<Shared.Entities.Classifieds.Posting>(dr, new Adams.Web.Data.ColMap { NameInDb = "PostingID", NameInObject = "ID" });
                    Posting.Status = (Shared.Entities.Classifieds.Status)dr["StatusID"].DbInt();
                    Posting.Category = new Shared.Entities.Classifieds.Category()
                    {
                        ID = dr["CategoryID"].DbInt(),
                        Name = dr["TypeName"].DbString(),
                        Type = new Shared.Entities.Classifieds.Type()
                        {
                            ID = dr["TypeID"].DbInt(),
                            Name = dr["TypeName"].DbString()
                        }
                    };

                    Posting.Member = new Shared.Entities.Membership.Member()
                    {
                        ID = dr["MemberID"].DbInt(),
                        Email = dr["Email"].DbString(),
                        FirstName = dr["FirstName"].DbString(),
                        LastName = dr["LastName"].DbString(),
                    };

                    data.Postings.Add(Posting);
                }
            }

            return data;
        }

        public static void Renew(int memberId, int postingId, DateTime expiresOn)
        {
            Adams.Web.Data.DB.Run(@"
UPDATE Classifieds.Postings
SET ExpiresOn = @ExpiresOn
WHERE PostingID = @PostingID AND MemberID = @MemberID
", CommandType.Text,
                    Adams.Web.Data.DB.CreateParameter("@PostingID", postingId, SqlDbType.Int),
                    Adams.Web.Data.DB.CreateParameter("@MemberID", memberId, SqlDbType.Int),
                    Adams.Web.Data.DB.CreateParameter("@ExpiresOn", expiresOn));
        }

        public static void Remove(int memberId, int postingId)
        {
            Adams.Web.Data.DB.Run(@"
UPDATE Classifieds.Postings
SET StatusID = 4
WHERE PostingID = @PostingID AND MemberID = @MemberID
", CommandType.Text,
                    Adams.Web.Data.DB.CreateParameter("@PostingID", postingId, SqlDbType.Int),
                    Adams.Web.Data.DB.CreateParameter("@MemberID", memberId, SqlDbType.Int));
        }
    }
}
